
import 'dart:math';
import 'package:ble2025_plugin/ble2203_plugin.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

///闹钟详情和添加
class AlarmClockPage extends StatefulWidget {

  Clock? clock;

  AlarmClockPage({Key? key,this.clock}) : super(key: key);

  @override
  State<AlarmClockPage> createState() => _AlarmClockPageState();
}

class _AlarmClockPageState extends State<AlarmClockPage> {

  bool enable = false;
  int hour = 0;
  int minute = 0;
  ///1 2 3
  int type = 1;

  List<bool> weeks = [false,false,false,false,false,false,false];



  @override
  void initState() {
    super.initState();
    if(widget.clock != null){
      setState(() {
        enable = widget.clock!.enable;
        hour = widget.clock!.hour;
        minute = widget.clock!.minute;
        type = widget.clock!.type;
        for(int i = 0; i < widget.clock!.weeks.length; i++){
          if(widget.clock!.weeks[i] == '1'){
            weeks[i] = true;
          }else{
            weeks[i] = false;
          }
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("1939W测试Demo".tr),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  onPressed: null,
                  child: Text('开关'.tr),
                ),
              )),
              Expanded(child: Container(
                margin: const EdgeInsets.only(right: 10),
                child: Switch(
                  value: enable,
                  onChanged: (flag){
                      setState(() {
                        enable = flag;
                      });
                  },
                ),
              ))
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('时间'.tr),
                  onPressed: (){
                    showTimePicker(context: context, initialTime: TimeOfDay.now()).then((value){
                      if(value != null){
                        setState(() {
                          hour = value.hour;
                          minute = value.minute;
                        });
                      }
                    });
                  },
                ),
              )),
              Expanded(child: Container(
                margin: const EdgeInsets.only(right: 10),
                child: ElevatedButton(
                  onPressed:null,
                  child: Text('$hour:$minute'),
                ),
              ))
            ],
          ),
          Container(
            margin: const EdgeInsets.only(top: 10),
            child: Text('    ${'闹钟类型'.tr}'),
          ),
          Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Radio(value: type, groupValue: 1, onChanged: (value){
                setState(() {
                  type = 1;
                });
              },),
              Text('普通'.tr),
              Radio(value: type, groupValue: 2, onChanged: (value){
                setState(() {
                  type = 2;
                });
              }),
              Text('吃药'.tr),
              Radio(value: type, groupValue: 3, onChanged: (value){
                setState(() {
                  type = 3;
                });
              }),
              Text('喝水'.tr),
            ],
          ),
          Container(
            margin: const EdgeInsets.only(top: 10),
            child: Text('    ${'星期选择'.tr}'),
          ),
          GridView(
            padding: const EdgeInsets.only(top: 10),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3,childAspectRatio: 4/1),
            shrinkWrap: true,
            children: [
              buildWeekWidget(0,'周日'.tr),
              buildWeekWidget(1,'周一'.tr),
              buildWeekWidget(2,'周二'.tr),
              buildWeekWidget(3,'周三'.tr),
              buildWeekWidget(4,'周四'.tr),
              buildWeekWidget(5,'周五'.tr),
              buildWeekWidget(6,'周六'.tr),
            ],
          ),
          Row(
            children: [
              Expanded(child: Container(
                margin: const EdgeInsets.only(left: 10,right: 10),
                child: ElevatedButton(
                  child: Text('保存'.tr),
                  onPressed: (){
                    var c = Clock(number: widget.clock != null ? widget.clock!.number: -1,
                        type: type,
                        hour: hour,
                        minute: minute,
                        content: widget.clock != null ? widget.clock!.content: '',
                        enable: enable,
                        weeks: weeks.map((e) => e?"1":"0").toList());
                    Get.back(result: c);
                  },
                ),
              )),
            ],
          ),
        ],
      ),
    );

  }

  int getWeekValue(List<bool> weeks){
    int v = 0;
    for(int i = 0; i < weeks.length; i ++){
      if(weeks[i]){
        v += pow(2, i).toInt();
      }
    }
    return v;
  }

  Widget buildWeekWidget(int index,String text){
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Checkbox(
          value: weeks[index],
          activeColor: Colors.blue, //选中时的颜色
          onChanged:(value){
            if(value != null){
              setState(() {
                weeks[index]=value;
              });
            }
          } ,
        ),
        Text(text)
      ],
    );
  }
}
